import os
import zipfile

from utils import LOG, path_validator

# uncompress the given zip file which is specified in the config dict
# then check if these files exist:
# 1. example.txt
class SourceFileValidator:
    def __init__(self, config, common_paths, use_zip):
        self.config = config
        self.common_paths = common_paths
        self.use_zip = use_zip

    def validate_zipfiles(self):
        # Uncompress the zip file specified in the config dict
        zip_filename, _ = os.path.splitext(self.config.get('example-filename'))
        output_dir = os.path.join(self.common_paths.get('generator-source-files-path'), zip_filename)
        path_validator(output_dir)
        if self.use_zip:
            zip_file_path = os.path.join(self.common_paths.get('generator-source-files-path'), self.config.get('example-filename'))
            with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
                zip_ref.extractall(output_dir)
        
        if not os.path.exists(os.path.join(output_dir, 'example.txt')):
            LOG.error('Validation failed: example.txt doesn\'t exist!')
            return None, False
        
        return output_dir, zip_filename, True
        
